from opcUaSrc.plc.dbOpr import *
from opcUaSrc.plc.csvOpr import *
from apscheduler.schedulers.background import BlockingScheduler,BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.executors.pool import ThreadPoolExecutor
from opcUaSrc.plc.poolClean import poolClean
from opcUaSrc.plc.uploadTools import *


if __name__ == '__main__':
    # 1. 链接PLC
    commonNodes = connect()

    # 2. 启动线程池
    executors = {
        'default': ThreadPoolExecutor(20)
    }
    main = BackgroundScheduler()
    main.add_job(saveDBData1, IntervalTrigger(seconds=Config.data1Span), id='saveDBData1', args=[commonNodes])
    # main.add_job(saveData1, IntervalTrigger(seconds=Config.data1Span), id='saveData1', args=[commonNodes])
    main.add_job(poolClean, IntervalTrigger(seconds=Config.cleanSpan), id='poolClean', args=[Config.poolRoot, Config.poolNum])
    main.add_job(yawUpload, IntervalTrigger(seconds=Config.data1Span), id='yawUpload', args=[commonNodes])
    main.add_job(pitchUpload, IntervalTrigger(seconds=Config.data1Span), id='pitchUpload', args=[commonNodes])
    main.add_job(torqueUpload, IntervalTrigger(seconds=Config.data1Span), id='torqueUpload', args=[commonNodes])
    main.start()
    

    while True:
        try:
            saveDatam(commonNodes, Config.poolRoot)
        except Exception as exp:
            print(f"Exception : [{exp}]")
            commonNodes = connect()
            continue


